package com.ruoyi.common.utils;


import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 签名验证工具
 */
public class SignUtil {

    /**
     *
     * 方法用途: 对所有传入参数按照字段名的Unicode码从小到大排序（字典序），并且生成url参数串<br>
     * 实现步骤: <br>
     *
     * @param paraMap   要排序的Map对象
     * @param urlEncode   是否需要URLENCODE
     * @return
     */
    public static String formatUrlMap(Map<String, String> paraMap, boolean urlEncode)  {
        String buff;
        Map<String, String> tmpMap = paraMap;
        List<Map.Entry<String, String>> infoIds = new ArrayList<>(tmpMap.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
        Collections.sort(infoIds, Comparator.comparing(o -> (o.getKey())));
        // 构造URL 键值对的格式
        StringBuilder buf = new StringBuilder();
        for (Map.Entry<String, String> item : infoIds) {
            if (!StringUtils.isEmpty(item.getKey())) {
                String key = item.getKey();
                String val = item.getValue();
                if (urlEncode) {
                    try {
                        val = URLEncoder.encode(val, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    }
                }
                buf.append(key + "=" + val);
                buf.append("&");
            }
        }
        buff = buf.toString();
        if (buff.isEmpty() == false) {
            buff = buff.substring(0, buff.length() - 1);
        }

        return buff;
    }

    /**
     * @author
     * @param payParams
     * @return
     */
    public static void buildPayParamsObj(StringBuilder sb,Map<String, Object> payParams,boolean encoding){
        List<String> keys = new ArrayList<String>(payParams.keySet());
        Collections.sort(keys);
        for(String key : keys){
            if (payParams.get(key) == null || String.valueOf(payParams.get(key)).equals("")) {
                continue;
            }
            sb.append(key).append("=");
            if(encoding){
                sb.append(urlEncode(String.valueOf(payParams.get(key))));
            }else{
                sb.append(String.valueOf(payParams.get(key)));
            }
            sb.append("&");
        }
        sb.setLength(sb.length() - 1);
    }


    public static void buildPayParamsObj(StringBuilder sb,Map<String, String> payParams){
        List<String> keys = new ArrayList<String>(payParams.keySet());
        Collections.sort(keys);
        for(String key : keys){
            if (payParams.get(key) == null || String.valueOf(payParams.get(key)).equals("")) {
                continue;
            }
            sb.append(key).append("=");

                sb.append(String.valueOf(payParams.get(key)));
            sb.append("&");
        }
        sb.setLength(sb.length() - 1);
    }

    public static String urlEncode(String str){
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (Throwable e) {
            return str;
        }
    }

}
